package com.example.myx264test;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.util.Log;
import android.util.Size;
import android.view.TextureView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.example.myx264test.databinding.ActivityMainBinding;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks{
    private final String TAG = "MainActivity";
    static {
        System.loadLibrary("myx264test");
    }

    private ActivityMainBinding binding;
    private CameraUtil mCameraUtil;
    private Button mBtn;
    private AutoFitTextureView mTextureView;
    private Size mPhone_size;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        mBtn = binding.btn;
        mTextureView = binding.autoFitTextureView;

        checkPermission();

        Point point = new Point();
        getWindowManager().getDefaultDisplay().getRealSize(point);
        int phone_width = point.x;
        int phone_height = point.y;
        mPhone_size = new Size(phone_width, phone_height);
        Log.e(TAG, "phone_width = "+phone_width+", phone_height = "+phone_height );
        mCameraUtil = new CameraUtil((CameraManager) getSystemService(Context.CAMERA_SERVICE), mPhone_size, this, mTextureView);
        mTextureView.setAspectRatio(mCameraUtil.getSize().getWidth(), mCameraUtil.getSize().getHeight());
        Log.e(TAG, "onCreate: width = "+mCameraUtil.getSize().getWidth()+", height = "+ mCameraUtil.getSize().getHeight());
        mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
                Log.e(TAG, "onSurfaceTextureAvailable: width = "+width+", height = "+height );
                mCameraUtil.openCamera(CameraUtil.CameraFaceing.LENS_FACING_BACK);
            }

            @Override
            public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {

            }
        });
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    private void checkPermission() {
        Log.e(TAG, "checkPermission: " );
        if (!EasyPermissions.hasPermissions(this, getPermissions())) {
            Log.e(TAG, "checkPermission: 申请权限" );
            EasyPermissions.requestPermissions(this, "程序运行需要存储权限", 100, getPermissions());
        }
    }


    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        Log.e(TAG, "onPermissionsGranted: requestCode = "+requestCode+", perms = "+perms );
        if (!mCameraUtil.isCameraOpened()) {
            Log.e(TAG, "onPermissionsGranted: openCamera" );
            if (mCameraUtil == null){
                mCameraUtil = new CameraUtil((CameraManager) getSystemService(Context.CAMERA_SERVICE), mPhone_size, this, mTextureView);
            }
            mCameraUtil.openCamera(CameraUtil.CameraFaceing.LENS_FACING_BACK);
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        Log.e(TAG, "onPermissionsDenied: requestCode = "+requestCode+", perms = "+perms );
    }

    public String[] getPermissions() {
        Log.e(TAG, "getPermissions: " );
        String[] permissions = {
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA,
                Manifest.permission.INTERNET
        };
        return permissions;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        Log.e(TAG, "onRequestPermissionsResult: requestCode = "+requestCode+", permissions = "+permissions+", grantResults = "+grantResults );
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // 将结果转发给 EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    protected void onDestroy() {
        Log.e(TAG, "onDestroy: " );
        super.onDestroy();
        mCameraUtil.close();
        native_close();
    }

    private native void native_close();
}